<?php

/**
 * MVM_MALL 网上商店系统  自定义导航管理
 * ============================================================================
 * 版权所有 (C) 2007-2010 www.mvmmall.com，并保留所有权利。
 * 网站地址: http://www.mvmmall.com
 * ----------------------------------------------------------------------------
 * 这是一个免费开源的软件；这意味着您可以在不用于商业目的的前提下对程序代码
 * 进行修改、使用和再发布。
 * ============================================================================
 * $Author:  www.mvmmall.com  $
 * $Date: 2008-07-14 $
 * $Id: navigation.inc.php www.mvmmall.com$
 * ---------------------------------------------
*/

if(!defined('MVMMALL') || !defined('IN_ADMINCP')) exit('Access Denied');
$nav_array = array
(
'head'    => '头部导航',
'middle'  => '中部导航',
'foot'    => '底部导航',
'help'    => '帮助中心'
);
if($action=='list'){
	require_once MVMMALL_ROOT.'./include/pager.class.php';
	switch ($pos){
		case 'head':  { $scarch_sql = "WHERE pos= 'head'"; break;}
		case 'foot':  { $scarch_sql = "WHERE pos= 'foot'"; break;}
		case 'middle':{ $scarch_sql = "WHERE pos= 'middle'"; break;}
		case 'help':  { $scarch_sql = "WHERE pos= 'help'"; break;}
		default:      { $scarch_sql = false; $pos='head';}
	}
	$total_count = $db->counter("{$tablepre}nav",$scarch_sql);
	$page        = $page ? (int)$page:1;
	$list_num    = 10;
	$rowset      = new Pager($total_count,$list_num,$page);
	$from_record = $rowset->_offset();
	$nav         = array();
	$rs	         = $db->query("SELECT * FROM `{$tablepre}nav` $scarch_sql ORDER BY view LIMIT $from_record,$list_num");
	while($rt    = $db->fetch_array($rs)){
		$style_array=explode("|",$rt['style']);
		$style_array[1] && $rt['title']="<b>".$rt['title']."</b>";
		$style_array[2] && $rt['title']="<i>".$rt['title']."</i>";
		$style_array[3] && $rt['title']="<u>".$rt['title']."</u>";
		$style_array[0] && $rt['title']="<font color=\"$style_array[0]\">".$rt['title']."</font>";
		$rt['pos']      =  $nav_array[$rt['pos']];
		$rt['edit']     = "admincp.php?module=$module&action=edit&uid=$rt[nid]&page=$page";
		$rt['del']      = "admincp.php?module=$module&action=del&uid=$rt[nid]&page=$page";
		$nav_rt[]       = $rt;
	}
	$page_list   = $rowset->link("admincp.php?module=$module&action=$action&pos=$pos&page=");
	require_once template('navigation');
	footer();
} elseif ($action=='add'||$action=='edit'){
	
	$colors = array('skyblue','royalblue','blue','darkblue','orange','orangered','crimson','red','firebrick','darkred','green',
	'limegreen','seagreen','teal','deeppink','tomato','coral','purple','indigo','burlywood','sandybrown','sienna','chocolate','silver');
	$nav_system = nav_system();
	foreach ($nav_system as $key=>$val){
	    $sys_arr[] = $val[2] ? $val[2] :  $val[0]  ;
	}                  
	$menu_sys = drop_menu($sys_arr,'add_nav',$title,'onchange="add_navi(this.value);"');
	if ($setp==1 && $_POST) {
		$view	  = (int)$view;
		$title	  = dhtmlchars($title);
		$style	  = $color."|".$b."|".$i."|".$u;
		$link	  = dhtmlchars($link);
		$alt	  = dhtmlchars($alt);
		$nav_type = (int)$nav_type;
		//图片上传
		if ($_FILES['nav_img']['name']!='') {
			$nav_img && file_unlink($nav_img);
			require_once MVMMALL_ROOT.'./include/upfile.class.php';
			$rowset  = new upfile('gif,jpg,png,bmp','images/banner/');
			$nav_img = $rowset->upload('nav_img');
		}
		if ($action=='add') {
			$db->query("INSERT INTO `{$tablepre}nav` (title,style,link,alt,target,pos,view,nav_type,nav_img) VALUES('$title','$style','$link','$alt','$target','$pos','$view','$nav_type','$nav_img')");
		} else if ($action=='edit'&&is_numeric($uid)) {
			$db->query("UPDATE `{$tablepre}nav` SET title='$title',style='$style',link='$link',alt='$alt',target='$target',pos='$pos',view='$view',nav_type='$nav_type',nav_img='$nav_img' WHERE nid='$uid'");
		}else {
			admin_msg('pass_worng');
		}
		$cache->put_cache('nav');
		admin_msg('success',"admincp.php?module=$module&action=list&mode=$mode&page=$page");
	}
	if ($action=='edit' && is_numeric($uid)) {
		$nav_rt = $db->get_one("SELECT * FROM `{$tablepre}nav` WHERE nid='$uid'");
		@extract($nav_rt,EXTR_OVERWRITE);
		$style_array    = explode("|",$style);
		$style_array[1] && $b_check = 'checked';
		$style_array[2] && $i_check = 'checked';
		$style_array[3] && $u_check = 'checked';
		$target==1 ? $blank_check = 'checked' : $self_check = 'checked';

	}else {
	    $self_check = 'checked';
	}
	switch ($pos){
			case 'head':  { $head_check   = 'checked'; break; }
			case 'middle':{ $middle_check = 'checked'; break; }
			case 'help':  { $help_check   = 'checked'; break; }
			default:      { $foot_check   = 'checked'; break; }
		}
	foreach($colors as $c){
		$ifselect=$c==$style_array[0] ? 'selected' : '';
		$color_select.="<option value=\"$c\" style=\"background-color:$c;color:$c\" $ifselect></option>";
	}
	require_once template('navigation_add');
	footer();
} elseif($action=='del'&&is_numeric($uid)){
    $rt_nav = $db->get_one("SELECT nav_img,nid FROM `{$tablepre}nav` WHERE  nid='$uid'");
    $rt_nav['nav_img'] && file_unlink($rt_nav['nav_img']);
	$db->query("DELETE FROM `{$tablepre}nav` WHERE nid='$uid'");
	$cache->put_cache('nav');
}
else {
	admin_msg('pass_worng');
}

//系统栏目
function nav_system()
{
	global $tablepre,$db;
	$nav_system = array(
	array('--',''),
	array('购物车','cart.php?action=list'),//购物车
	array('最新商品','goods.php?action=new'),//最新商品
	array('推荐商品','goods.php?action=best'),//
	array('热门商品','goods.php?action=hot'),//热门商品
	array('促销商品','goods.php?action=sales'),//促销商品
	array('团购活动','group.php?action=list'),//团购活动
	array('拍卖活动','auction.php?action=list'),//拍卖活动
	array('品牌展示','brand.php?action=list'),//品牌展示
	array('缺货登记','lackcheck.php'),//缺货登记
	array('报价单','quotation.php?action=list'),//报价单
	array('商品搜索','search.php?action=search'),//商品搜索
	array('定单搜索','order.php?action=list'),//定单搜索
	array('商城导航','sitemap.php'),//商城导航
	array('标签云','tag.php?action=list')//标签云
	);
	$nav_system[] = array('--','--');
	foreach ($GLOBALS['cache']->get_cache('tree') as $key=>$val){
	    $val['url'] = "category.php?action=$key";
	    $nav_system[] = array($val[0],"category.php?action=$key",str_repeat( "--", $val[2]).$val[0]);
	}
	$result = $db->query("SELECT uid,board_name_code,board_title FROM `{$tablepre}badmin_table` ORDER BY uid DESC");
	while($rt = $db->fetch_array($result)) {
		$nav_system[] = array($rt['board_title'],"board.php?action=$rt[board_name_code]");
	}
	/**end**/
	/**页面**/
	$nav_system[] = array('--','--');
	$result = $db->query("SELECT uid,page_subject,page_name FROM `{$tablepre}page_table` ORDER BY uid DESC");
	while($rt = $db->fetch_array($result)) {
		$nav_system[] = array($rt['page_subject'],"page.php?action=$rt[page_name]");
	}
	/**end**/
	return $nav_system;
}